//
//  AgeInfoView.m
//  TestAge2016(第六组)
//
//  Created by hp on 16/4/12.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "AgeInfoView.h"
// 省略mas_开头
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry.h>
@interface AgeInfoView ()
@property(nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UIImageView *genderImageView;
@end
@implementation AgeInfoView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (UILabel *)ageLabel
{
    if (_ageLabel==nil) {
        _ageLabel = [[UILabel alloc] init];
        _ageLabel.textColor = [UIColor orangeColor];
        _ageLabel.font = [UIFont systemFontOfSize:150];
        _ageLabel.adjustsFontSizeToFitWidth = YES;
        _ageLabel.minimumScaleFactor = 0.1;
        _ageLabel.textAlignment = NSTextAlignmentCenter;
        _ageLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
        [self addSubview:_ageLabel];
    }
    return _ageLabel;
}

- (UIImageView *)genderImageView
{
    if (_genderImageView==nil) {
        _genderImageView = [[UIImageView alloc] init];
        [self addSubview:_genderImageView];
        
    }
    return _genderImageView;
}

- (void)setAge:(int)age
{
    _age = age;
    self.ageLabel.text = [NSString stringWithFormat:@"%d",_age];
    [self setNeedsDisplay];
}

- (void)setMale:(BOOL)male
{
    _male = male;
    self.genderImageView.image = [UIImage imageNamed:male?@"icon-gender-male":@"icon-gender-female"];
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 0, 0);
    CGContextAddLineToPoint(ctx, width, 0);
    CGContextAddLineToPoint(ctx, width,height-width/6.0f);
    CGContextAddLineToPoint(ctx, width/2.0f+width/6.0f, height-width/6.0f);
    CGContextAddLineToPoint(ctx, width/2.0f, height);
    CGContextAddLineToPoint(ctx, width/2.0f-width/6.0f, height-width/6.0f);
    CGContextAddLineToPoint(ctx, 0, height-width/6.0f);
    CGContextClosePath(ctx);
    
    [[UIColor yellowColor] set];
    CGContextFillPath(ctx);
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self.genderImageView remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.left).offset(5);
        make.top.equalTo(self.top).offset(5);
        make.right.equalTo(self.ageLabel.left).offset(-5);
        make.bottom.equalTo(self.bottom).offset(-20);
    }];
    
    [self.ageLabel remakeConstraints:^(MASConstraintMaker *make) {
       
        make.width.equalTo(self.genderImageView.width);
        make.height.equalTo(self.genderImageView.height);
        make.centerY.equalTo(self.genderImageView.centerY);
        make.right.equalTo(self.right).offset(-5);
    }];
    
}
@end
